package com.dp;

public class Solution35 {
    public int maxProfit(int[] prices) {
        if (prices.length==1) return 0;
        int dpi0 = 0;
        int dpi1 = -prices[0];
        int dpi2 = 0;
        int dpi3 = -prices[0];
        int dpi4 = 0;
        int tmp1 = dpi1;
        int tmp2 = dpi2;
        int tmp3 = dpi3;
        for (int i = 1; i < prices.length; i++) {
            dpi1 = Math.max(dpi1,dpi0-prices[i]);
            dpi2 = Math.max(dpi2,tmp1+prices[i]);
            dpi3 = Math.max(dpi3,tmp2-prices[i]);
            dpi4 = Math.max(dpi4,tmp3+prices[i]);
            tmp1 = dpi1;
            tmp2 = dpi2;
            tmp3 = dpi3;
        }
        return dpi4;
    }
}
